package com.zfei.gfb.request;

import cn.hutool.core.lang.Validator;
import com.zfei.gfb.bean.Const;
import com.zfei.gfb.bean.Status;
import com.zfei.gfb.bean.StatusException;
import com.zfei.gfb.util.ObjUtil;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.util.List;


/**
 * @author fgh
 */
@Data
public class GfbGoodsAddRequest {


    // 物品名称
    @ApiModelProperty(value = "物品名称", required = true)
    private String name;
    // 售卖方式：1:工分兑换 2:面议
    @ApiModelProperty(value = "售卖方式：1:工分兑换 2:面议", required = true)
    private Integer saleType;
    // 售卖地址
    @ApiModelProperty(value = "售卖地址", required = true)
    private String address;
    // 联系人电话
    @ApiModelProperty(value = "联系人电话", required = true)
    private String phone;
    // 物品价格，工分
    @ApiModelProperty(value = "工分", required = true)
    private Integer price;
    // 物品描述
    @ApiModelProperty(value = "物品描述", required = true)
    private String content;
    // 图片url集合,json数组形式
    @ApiModelProperty(value = "图片url集合", required = true)
    private List<String> images;

    @ApiModelProperty(value = "村民用户id", required = true)
    private Integer userId;

    public void checkInsertNull() {
        if (ObjUtil.anyNull(name, saleType, address, phone, content, images)) {
            throw new StatusException(Status.INVALID_ARGUMENT);
        }
        if (saleType == Const.SALE_TYPE_ON) {
            if (price == null) {
                throw new StatusException(Status.INVALID_ARGUMENT);
            }
        }
        if (content.length() > Const.CONTENT_LENGTH) {
            throw new StatusException(Status.INVALID_ARGUMENT);
        }
        if (images.size() > Const.IMAGE_SIZE) {
            throw new StatusException(Status.INVALID_ARGUMENT);
        }
        for (String image : images) {
            if (!Validator.isUrl(image)) {
                throw new StatusException(Status.INVALID_ARGUMENT);
            }
        }
    }

}

